package com.aube.interact.fighter.card;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.aube.interact.fighter.R;
import com.aube.interact.fighter.ViewGenerator;
import com.aube.interact.fighter.model.Coordinate;
import com.aube.interact.fighter.model.OptionResultModel;
import com.aube.interact.fighter.model.ReportResultModel;
import com.aube.interact.fighter.model.TimeLinePiece;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.model.ChannelDetail;

/**
 * Created by huyaonan on 16/11/15.
 */
public class CardInfoThumb extends BaseCardNew {

    private ImageView mImg;

    public CardInfoThumb(Context context) {
        this(context, null);
    }

    public CardInfoThumb(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CardInfoThumb(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.aube_intera_card_info_thumb, this, true);

        mImg = (ImageView) findViewById(R.id.info_thumbnail);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(iSender != null)
                    dismissSelfAndShowDetail();
            }
        });
    }

    @Override
    public void init(TimeLinePiece data, ChannelDetail channelDetail) {
        super.init(data, channelDetail);

        ImageLoader.getInstance(getContext()).fillImage(mImg, mData.detail.getFragmentThumbnail());
    }

    private void dismissSelfAndShowDetail() {
        Animation anim = AnimationUtils.loadAnimation(getContext(), mOutAnim);
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.GONE);
                setAlpha(0f);
                mData.showInfoDetail = true;
                iSender.showInfoCard(mData);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        startAnimation(anim);
    }

    public void animIn(RelativeLayout parent, Animation.AnimationListener listener) {
        Coordinate coordinate = mData.detail.getThumbnailPosition();
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(coordinate == null) {
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        } else {
            //absolute position
            updatePosition();
        }
        parent.addView(this, params);

        generateAnim();

        Animation anim = AnimationUtils.loadAnimation(getContext(), mInAnim);
        anim.setAnimationListener(listener);
        startAnimation(anim);
    }

    /**
     * TODO 需要考虑到卡片是否超出屏幕
     */
    public void updatePosition() {
        if(mData != null) {
            Coordinate coordinate = mData.detail.getThumbnailPosition();
            indexX = coordinate.pointxPercent;
            indexY = coordinate.pointyPercent;

            if(indexX >=0)
                setTranslationX(mScreenWidth*indexX);
            if(indexY >=0)
                setTranslationY(mScreenHeight*indexY);
        }
    }

    @Override
    public String getType() {
        return TYPE_INFO;
    }

    @Override
    public void doNext() {
    }

    @Override
    public void syncData(OptionResultModel response) {
    }

    private boolean mBPreviewDataSynced = false;

    @Override
    public boolean IsPreviewDataSynced() {
        return mBPreviewDataSynced;
    }

    @Override
    public void syncPreviewData(ReportResultModel result) {
    }

}
